const express = require("express");

/* 登录校验中间件 */
const loginCheck = require("../middlewares/loginCheck");
// const bodyParser = require("body-parser");
// 创建 application/x-www-form-urlencoded 编码解析
// const urlencodedParser = bodyParser.urlencoded({ extended: false });

const router = express.Router();

/* 定义路由接口 */
// GET /
router.get("/", function (req, res) {
    res.send("Hello From indexRouter");
});

// GET http://localhost:8002/get?a=12&b=34
router.get("/get", function (req, res) {
    res.send(
        JSON.stringify({
            ...req.query,
        })
    );
});

// POST http://localhost:8002/post
// router.post("/post", urlencodedParser, function (req, res) {
router.post("/post", function (req, res) {
    // res.send(
    //     JSON.stringify({
    //         msg:"msg from /post",
    //         ...req.body
    //     })
    // );

    res.json({
        msg: "msg from /post",
        ...req.body,
    });
});

// GET /headers
router.get("/headers", (req, res, next) => {
    res.json({
        headers: req.headers,
    });
});

/* 使用自定义中间件 */
router.get("/testtoken", loginCheck, (req, res, next) => res.end("test ok"));

module.exports = router;
